﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:Classes
	/// </summary>
	public partial class Classes
	{
		public Classes()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "Classes"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Classes");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.Classes model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Classes(");
			strSql.Append("Name,SchoolId,RoomID,CreateUserID,CreateTime,Manager,Contents,GongGao,Remark1,Remark2,Pesonnel,Remark3)");
			strSql.Append(" values (");
			strSql.Append("@Name,@SchoolId,@RoomID,@CreateUserID,@CreateTime,@Manager,@Contents,@GongGao,@Remark1,@Remark2,@Pesonnel,@Remark3)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,200),
					new SqlParameter("@SchoolId", SqlDbType.Int,4),
					new SqlParameter("@RoomID", SqlDbType.VarChar,200),
					new SqlParameter("@CreateUserID", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Manager", SqlDbType.VarChar,200),
					new SqlParameter("@Contents", SqlDbType.NVarChar),
					new SqlParameter("@GongGao", SqlDbType.NVarChar),
					new SqlParameter("@Remark1", SqlDbType.NVarChar),
					new SqlParameter("@Remark2", SqlDbType.NVarChar),
					new SqlParameter("@Pesonnel", SqlDbType.VarChar,500),
					new SqlParameter("@Remark3", SqlDbType.NVarChar)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.SchoolId;
			parameters[2].Value = model.RoomID;
			parameters[3].Value = model.CreateUserID;
			parameters[4].Value = model.CreateTime;
			parameters[5].Value = model.Manager;
			parameters[6].Value = model.Contents;
			parameters[7].Value = model.GongGao;
			parameters[8].Value = model.Remark1;
			parameters[9].Value = model.Remark2;
			parameters[10].Value = model.Pesonnel;
			parameters[11].Value = model.Remark3;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.Classes model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Classes set ");
			strSql.Append("Name=@Name,");
			strSql.Append("SchoolId=@SchoolId,");
			strSql.Append("RoomID=@RoomID,");
			strSql.Append("CreateUserID=@CreateUserID,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("Manager=@Manager,");
			strSql.Append("Contents=@Contents,");
			strSql.Append("GongGao=@GongGao,");
			strSql.Append("Remark1=@Remark1,");
			strSql.Append("Remark2=@Remark2,");
			strSql.Append("Pesonnel=@Pesonnel,");
			strSql.Append("Remark3=@Remark3");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,200),
					new SqlParameter("@SchoolId", SqlDbType.Int,4),
					new SqlParameter("@RoomID", SqlDbType.VarChar,200),
					new SqlParameter("@CreateUserID", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Manager", SqlDbType.VarChar,200),
					new SqlParameter("@Contents", SqlDbType.NVarChar),
					new SqlParameter("@GongGao", SqlDbType.NVarChar),
					new SqlParameter("@Remark1", SqlDbType.NVarChar),
					new SqlParameter("@Remark2", SqlDbType.NVarChar),
					new SqlParameter("@Pesonnel", SqlDbType.VarChar,500),
					new SqlParameter("@Remark3", SqlDbType.NVarChar),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.SchoolId;
			parameters[2].Value = model.RoomID;
			parameters[3].Value = model.CreateUserID;
			parameters[4].Value = model.CreateTime;
			parameters[5].Value = model.Manager;
			parameters[6].Value = model.Contents;
			parameters[7].Value = model.GongGao;
			parameters[8].Value = model.Remark1;
			parameters[9].Value = model.Remark2;
			parameters[10].Value = model.Pesonnel;
			parameters[11].Value = model.Remark3;
			parameters[12].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Classes ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Classes ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.Classes GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,Name,SchoolId,RoomID,CreateUserID,CreateTime,Manager,Contents,GongGao,Remark1,Remark2,Pesonnel,Remark3 from Classes ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			Model.Classes model=new Model.Classes();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["SchoolId"]!=null && ds.Tables[0].Rows[0]["SchoolId"].ToString()!="")
				{
					model.SchoolId=int.Parse(ds.Tables[0].Rows[0]["SchoolId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RoomID"]!=null && ds.Tables[0].Rows[0]["RoomID"].ToString()!="")
				{
					model.RoomID=ds.Tables[0].Rows[0]["RoomID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateUserID"]!=null && ds.Tables[0].Rows[0]["CreateUserID"].ToString()!="")
				{
					model.CreateUserID=int.Parse(ds.Tables[0].Rows[0]["CreateUserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Manager"]!=null && ds.Tables[0].Rows[0]["Manager"].ToString()!="")
				{
					model.Manager=ds.Tables[0].Rows[0]["Manager"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Contents"]!=null && ds.Tables[0].Rows[0]["Contents"].ToString()!="")
				{
					model.Contents=ds.Tables[0].Rows[0]["Contents"].ToString();
				}
				if(ds.Tables[0].Rows[0]["GongGao"]!=null && ds.Tables[0].Rows[0]["GongGao"].ToString()!="")
				{
					model.GongGao=ds.Tables[0].Rows[0]["GongGao"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark1"]!=null && ds.Tables[0].Rows[0]["Remark1"].ToString()!="")
				{
					model.Remark1=ds.Tables[0].Rows[0]["Remark1"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark2"]!=null && ds.Tables[0].Rows[0]["Remark2"].ToString()!="")
				{
					model.Remark2=ds.Tables[0].Rows[0]["Remark2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Pesonnel"]!=null && ds.Tables[0].Rows[0]["Pesonnel"].ToString()!="")
				{
					model.Pesonnel=ds.Tables[0].Rows[0]["Pesonnel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark3"]!=null && ds.Tables[0].Rows[0]["Remark3"].ToString()!="")
				{
					model.Remark3=ds.Tables[0].Rows[0]["Remark3"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,Name,SchoolId,RoomID,CreateUserID,CreateTime,Manager,Contents,GongGao,Remark1,Remark2,Pesonnel,Remark3 ");
			strSql.Append(" FROM Classes ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,Name,SchoolId,RoomID,CreateUserID,CreateTime,Manager,Contents,GongGao,Remark1,Remark2,Pesonnel,Remark3 ");
			strSql.Append(" FROM Classes ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>*/
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Classes";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}

		#endregion  Method
	}
}

